#!/bin/sh

username="root"
password="root123"
src_file="/ubi/log"
BIN_DIR="/usr/bin"

#remove the previous all log
rm -rf /mnt/usr/alllog*

#create directory for log, base on year,month,day,hour,minute,second
current=`date +%Y%m%d%H%M%S`
rm -rf /mnt/usr/alllog_$current
mkdir -p /mnt/usr/alllog_$current


#save the x86 log first 
cd /mnt/usr/alllog_$current
cp -rf /mnt/usr/prelog.tar.gz log_oldest.tar.gz
cp -rf /mnt/usr/log.tar.gz log_lasttime.tar.gz
cp -rf /mnt/usr/log .

tar cvzf log_newest.tar.gz ./log/
rm -rf ./log

hostlist=`arp -a | awk '{print $2}' | sed s/\(//g |sed s/\)//g`

echo "$hostlist"
for i in $hostlist
do
    result=`echo $i |grep "21"`

    echo "result $result"

    if [ "$result" != "" ]; then
        ping=`ping -c 3 $i |awk 'NR==7 {print $4}'`
        
        echo "$ping"

        if [ $ping -eq 0 ]; then
            echo "$i can not be connected!"
        else
            dir=`echo $i | awk -F . '{print $4}'`
            echo "$dir"
            cd /mnt/usr/alllog_$current
            mkdir -p $dir
            dest_file=/mnt/usr/alllog_$current/$dir

            $BIN_DIR/scpbbu $i $username $password $src_file $dest_file
        fi
    fi
done

cd /mnt/usr/

tar cvzf alllog_$current.tar.gz /mnt/usr/alllog_$current

echo "log collection done!"
